{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4 5 6 7 8 9]]\n",
      "<class 'numpy.matrix'>\n",
      "[[123]\n",
      " [456]\n",
      " [789]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "matr1=np.mat(\"1 2 3 4 5 6 7 8 9\")\n",
    "print(matr1)\n",
    "print(type(matr1))\n",
    "matr2=np.matrix([[123],[456],[789]])\n",
    "print(matr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[[3. 0. 0.]\n",
      " [0. 3. 0.]\n",
      " [0. 0. 3.]]\n",
      "[[1. 0. 0. 3. 0. 0.]\n",
      " [0. 1. 0. 0. 3. 0.]\n",
      " [0. 0. 1. 0. 0. 3.]\n",
      " [1. 0. 0. 3. 0. 0.]\n",
      " [0. 1. 0. 0. 3. 0.]\n",
      " [0. 0. 1. 0. 0. 3.]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-31\n",
    "arr1=np.eye(3)\n",
    "print(arr1)\n",
    "arr2=3*arr1\n",
    "print(arr2)\n",
    "arr22=np.bmat(\"arr1 arr2;arr1 arr2\")\n",
    "print(arr22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  6  9]\n",
      " [12 15 18]\n",
      " [21 24 27]]\n",
      "[[ 4  8 12]\n",
      " [16 20 24]\n",
      " [28 32 36]]\n",
      "[[ -2  -4  -6]\n",
      " [ -8 -10 -12]\n",
      " [-14 -16 -18]]\n",
      "[[ 90 108 126]\n",
      " [198 243 288]\n",
      " [306 378 450]]\n",
      "[[  3  12  27]\n",
      " [ 48  75 108]\n",
      " [147 192 243]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-32\n",
    "matr1=np.mat(\"1 2 3; 4 5 6; 7 8 9\")\n",
    "matr2=matr1*3\n",
    "print(matr2)\n",
    "print(matr1+matr2)\n",
    "print(matr1-matr2)\n",
    "print(matr1*matr2)\n",
    "print(np.multiply(matr1,matr2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n",
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n",
      "[[ 3.15251974e+15 -6.30503948e+15  3.15251974e+15]\n",
      " [-6.30503948e+15  1.26100790e+16 -6.30503948e+15]\n",
      " [ 3.15251974e+15 -6.30503948e+15  3.15251974e+15]]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[ 0.   1.  -0.5]\n",
      " [ 0.   2.  -1. ]\n",
      " [ 0.   3.   2.5]]\n"
     ]
    }
   ],
   "source": [
    "print(matr1.T)\n",
    "print(matr1.H)\n",
    "print(matr1.I)\n",
    "print(matr1.A)\n",
    "print(matr1)\n",
    "print(matr1*matr1.I)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False]\n",
      "[ True False False]\n"
     ]
    }
   ],
   "source": [
    "#代码2-35\n",
    "x=np.array([1,3,5])\n",
    "y=np.array([2,3,5])\n",
    "print(x<y)\n",
    "print(x!=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#代码 2-36\n",
    "print(np.all(x==y))\n",
    "print(np.any(x==y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [1 1 1]\n",
      " [2 2 2]\n",
      " [3 3 3]]\n",
      "(4, 3)\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "#代码 2-37\n",
    "arr1=np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])\n",
    "print(arr1)\n",
    "print(arr1.shape)\n",
    "arr2=np.array([1,2,3])\n",
    "print(arr1+arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]]\n",
      "[[1 1 1]\n",
      " [3 3 3]\n",
      " [5 5 5]\n",
      " [7 7 7]]\n"
     ]
    }
   ],
   "source": [
    "#代码 2-38\n",
    "arr1=np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])\n",
    "arr2=np.array([1,2,3,4]).reshape(4,1)\n",
    "print(arr2)\n",
    "print(arr1+arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7  8  9]\n",
      " [10 11 12 13 14 15 16 17 18 19]\n",
      " [20 21 22 23 24 25 26 27 28 29]\n",
      " [30 31 32 33 34 35 36 37 38 39]\n",
      " [40 41 42 43 44 45 46 47 48 49]\n",
      " [50 51 52 53 54 55 56 57 58 59]\n",
      " [60 61 62 63 64 65 66 67 68 69]\n",
      " [70 71 72 73 74 75 76 77 78 79]\n",
      " [80 81 82 83 84 85 86 87 88 89]\n",
      " [90 91 92 93 94 95 96 97 98 99]]\n",
      "[[ 0  1  2  3  4  5  6  7  8  9]\n",
      " [10 11 12 13 14 15 16 17 18 19]\n",
      " [20 21 22 23 24 25 26 27 28 29]\n",
      " [30 31 32 33 34 35 36 37 38 39]\n",
      " [40 41 42 43 44 45 46 47 48 49]\n",
      " [50 51 52 53 54 55 56 57 58 59]\n",
      " [60 61 62 63 64 65 66 67 68 69]\n",
      " [70 71 72 73 74 75 76 77 78 79]\n",
      " [80 81 82 83 84 85 86 87 88 89]\n",
      " [90 91 92 93 94 95 96 97 98 99]]\n"
     ]
    }
   ],
   "source": [
    "import numpy  as np\n",
    "arr=np.arange(100).reshape(10,10)\n",
    "np.save(\"save_arr\",arr)\n",
    "#np.save(\"C:/tmp/save_arr\",arr)\n",
    "print(arr)\n",
    "loaded_data = np.load(\"save_arr.npy\")\n",
    "print(loaded_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "#代码 2-40\n",
    "arr1=np.array([[1,2,3],[4,5,6]])\n",
    "arr2=np.arange(0,1.0,0.1)\n",
    "np.savez(\"savez_arr\",arr1,arr2)\n",
    "print(arr1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7  8  9]\n",
      " [10 11 12 13 14 15 16 17 18 19]\n",
      " [20 21 22 23 24 25 26 27 28 29]\n",
      " [30 31 32 33 34 35 36 37 38 39]\n",
      " [40 41 42 43 44 45 46 47 48 49]\n",
      " [50 51 52 53 54 55 56 57 58 59]\n",
      " [60 61 62 63 64 65 66 67 68 69]\n",
      " [70 71 72 73 74 75 76 77 78 79]\n",
      " [80 81 82 83 84 85 86 87 88 89]\n",
      " [90 91 92 93 94 95 96 97 98 99]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "#代码2-41\n",
    "loaded_data =np.load(\"save_arr.npy\")\n",
    "print(loaded_data)\n",
    "\n",
    "loaded_data1=np.load(\"savez_arr.npz\")\n",
    "print(loaded_data1['arr_0'])\n",
    "print(loaded_data1['arr_1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.   0.5  1.   1.5  2.   2.5]\n",
      " [ 3.   3.5  4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5  8.   8.5]\n",
      " [ 9.   9.5 10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "source": [
    "#代码 2-42\n",
    "arr10=np.arange(0,12,0.5).reshape(4,-1)\n",
    "print(arr10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 4 8 5 7 3 7 8 5 4]\n",
      "None\n",
      "[3 4 4 5 5 7 7 7 8 8]\n"
     ]
    }
   ],
   "source": [
    "#代码2-44\n",
    "np.random.seed(42)\n",
    "arr=np.random.randint(1,10,size=10)\n",
    "print(arr)\n",
    "\n",
    "print(arr.sort())\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8 8 3]\n",
      " [6 5 2]\n",
      " [8 6 2]]\n",
      "[[3 8 8]\n",
      " [2 5 6]\n",
      " [2 6 8]]\n",
      "[[2 5 6]\n",
      " [2 6 8]\n",
      " [3 8 8]]\n",
      "[[2 5 6]\n",
      " [2 6 8]\n",
      " [3 8 8]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.random.randint(1,10,size=(3,3))\n",
    "print(arr)\n",
    "arr.sort()\n",
    "print(arr)\n",
    "\n",
    "arr.sort(axis=0)\n",
    "print(arr)\n",
    "\n",
    "arr.sort(axis=1)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "#代码2-48\n",
    "arr=np.arange(5)\n",
    "print(arr)\n",
    "print(np.tile(arr,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "190\n",
      "190\n",
      "[30 34 38 42 46]\n",
      "[10 35 60 85]\n"
     ]
    }
   ],
   "source": [
    "#代码2-50\n",
    "arr=np.arange(20).reshape(4,5)\n",
    "print(arr)\n",
    "print(np.sum(arr))\n",
    "print(arr.sum())\n",
    "print(arr.sum(axis=0))\n",
    "print(arr.sum(axis=1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
